package cn.jinjimi.app.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.app.R;

/**
 * Created by Roy
 * Date: 16/6/2
 */
public class GuideDialog extends Dialog {

    private int resId;
    private String shareKey;
    private ScalingUtils.ScaleType scaleType;

    public static class Builder {
        int resId;
        String shareKey;
        ScalingUtils.ScaleType scaleType = ScalingUtils.ScaleType.CENTER;
        int styleId = R.style.dialog_full_transparent;
        int layoutId = R.layout.dialog_guide_layout;

        public Builder() {
        }

        public Builder DrawableId(int resId) {
            this.resId = resId;
            return this;
        }

        public Builder SharePreferenceKey(String shareKey) {
            this.shareKey = shareKey;
            return this;
        }

        public Builder ScaleType(ScalingUtils.ScaleType scaleType) {
            this.scaleType = scaleType;
            return this;
        }

        public Builder Style(int styleId) {
            this.styleId = styleId;
            return this;
        }

        public Builder layoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public GuideDialog build(Context context) {
            return new GuideDialog(context, this.layoutId, this.styleId, this.resId, this.shareKey, this.scaleType);
        }
    }

    public GuideDialog(Context context, int layoutId, int styleId, int resId, String shareKey, ScalingUtils.ScaleType scaleType) {
        super(context, styleId);

        this.resId = resId;
        this.shareKey = shareKey;
        this.scaleType = scaleType;

        setContentView(layoutId);
        setCanceledOnTouchOutside(true);

        initView();
    }

    private void initView() {
        ImageView imgGuide = (ImageView) findViewById(R.id.img_guide);

        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(getContext().getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setActualImageScaleType(scaleType)
                .build();

        if (imgGuide != null && resId > 0) {
            ((SimpleDraweeView) imgGuide).setHierarchy(hierarchy);
            imgGuide.setImageURI(Uri.parse("res:///" + resId));
        }

        findViewById(R.id.layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                PreferenceManager.setData(shareKey, true);
            }
        });
    }

}
